/******************************************************************************
 *
 * ngcap - network sniffer using raw sockets
 *
 * Author:        cr
 * E-mail:        cr.at.netgarage.org
 * WWW:           http://www.netgarage.org
 * Copyright (C): 2009 by cr
 *
 * This file is part of ngcap.
 *
 * ngcap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ngcap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ngcap.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

#ifndef _RAWSOCKET_HPP_
#define _RAWSOCKET_HPP_

//#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
//#include <string>
#include <stdexcept>

struct SocketError : std::runtime_error
{
    SocketError(const std::string& what_arg) : std::runtime_error(what_arg) {}
};

class RawSocket
{
public:
	RawSocket(int st=SOCK_RAW, int p=ETH_P_ALL);
    ~RawSocket();
	ssize_t receive(void* data, int d=AF_INET);
	void setInterface(const std::string& intf);

private:
	enum SocketState { UNDEFINED, CREATED, CLOSED };
	int sockfd;
	int socketType;
	int protocol;
	int domain;
	struct sockaddr_ll addr;
    struct ifreq ifr;
	SocketState state;
    static const int MTU=1518;
	RawSocket(const RawSocket&) {};
	RawSocket& operator=(const RawSocket& sock);
};
#endif
